package cn.tedu.vrd07.controller;

import cn.tedu.vrd07.entity.Product;
import cn.tedu.vrd07.entity.User;
import cn.tedu.vrd07.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;
    @Value("${dirPath}")
    private String dirPath;
    @RequestMapping("insert")
    public int insert(Product product, MultipartFile picFile, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        System.out.println("product = " + product + ", picFile = " + picFile);
        String fileName=picFile.getOriginalFilename();
        String suffix=fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //得到和日期相关的路径
        SimpleDateFormat format=new SimpleDateFormat("/yyyy/MM/dd/");
        //把当前时间按照指定的格式进行转换/2021/10/19/
        String dataPath =format.format(new Date());
        //得到保存图片文件夹的路径e:/upload/vrd/2021/10/19/
        File dirFile =new File(dirPath+dataPath);
        //如果不存在则创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到完整的文件路径
        String filePath=dirPath+dataPath+fileName;
        //保存上传的文件
        picFile.transferTo(new File(filePath));
        //图片路径添加到product对象里面
        product.setUrl(dataPath+fileName);
        product.setCreated(new Date());
        mapper.insert(product);
        return 1;
    }
    @RequestMapping("select")
    public List<Product> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(int id){
        //通过id查url
        String url=mapper.selectUrlById(id);
        String filePath=dirPath+url;
        new File(filePath).delete();
        mapper.delete(id);
    }
    @RequestMapping("selectforindex")
    public List<Product> selectForindex(){
        return mapper.selectForindex();
    }
    @RequestMapping("selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }
    @RequestMapping("selectByKw")
    public List<Product> selectByKw(String kw){
        return mapper.selectByKw(kw);
    }
    @RequestMapping("selectView")
    public List<Product> selectView(){
        return mapper.selectView();
    }
    @RequestMapping("selectLike")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }
    @RequestMapping("detail")
    public Product detail(int id,HttpSession session){
        String info = (String) session.getAttribute("view"+id);
        if (info==null){
            //浏览量+1
            mapper.viewById(id);
            session.setAttribute("view"+id,"浏览过了！");

        }
        return  mapper.detail(id);
    }
    @RequestMapping("like")
    public int like(int id ,HttpSession session){
        String info = (String) session.getAttribute("like"+id);
        if (info==null){
            mapper.likeById(id);
            session.setAttribute("like"+id,"点过赞了！");
            return 1;//点赞成功
        }
        return 2;//点过赞了
    }
}
